{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ortools.sat.python import cp_model\n",
    "\n",
    "model = cp_model.CpModel()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "name": "#%%"
    }
   },
   "outputs": [],
   "source": [
    "# 定义变量\n",
    "x1 = model.NewIntVar(0, 1000, 'x1')\n",
    "x2 = model.NewIntVar(0, 1000, 'x2')\n",
    "x3 = model.NewIntVar(0, 1000, 'x3')\n",
    "x4 = model.NewIntVar(0, 1000, 'x4')\n",
    "x5 = model.NewIntVar(0, 1000, 'x5')\n",
    "x6 = model.NewIntVar(0, 1000, 'x6')\n",
    "x7 = model.NewIntVar(0, 1000, 'x7')\n",
    "x8 = model.NewIntVar(0, 1000, 'x8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<ortools.sat.python.cp_model.Constraint at 0x186ccb666a0>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 添加约束\n",
    "model.Add(2*x1 + x2 + x3 + x4 >=100)\n",
    "model.Add(2*x2 + x3+ 3*x5 + 2*x6 + x7 >=150)\n",
    "model.Add(x1 + x3 + 3*x4 + 2*x6 + 3*x7 + 5*x8 >=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 添加目标函数\n",
    "model.Minimize(5*x1 + 6*x2 + 23*x3 + 5*x4 + 24*x5 + 6*x6 + 23*x7 + 5*x8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求解\n",
    "solver = cp_model.CpSolver()\n",
    "solver.Solve(model)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "目标函数值是： 600.0\n",
      "x1 =  0\n",
      "x2 =  75\n",
      "x3 =  0\n",
      "x4 =  25\n",
      "x5 =  0\n",
      "x6 =  0\n",
      "x7 =  0\n",
      "x8 =  5\n"
     ]
    }
   ],
   "source": [
    "print('目标函数值是：', solver.ObjectiveValue())\n",
    "print('x1 = ', solver.Value(x1))\n",
    "print('x2 = ', solver.Value(x2))\n",
    "print('x3 = ', solver.Value(x3))\n",
    "print('x4 = ', solver.Value(x4))\n",
    "print('x5 = ', solver.Value(x5))\n",
    "print('x6 = ', solver.Value(x6))\n",
    "print('x7 = ', solver.Value(x7))\n",
    "print('x8 = ', solver.Value(x8))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}